#include "TestObject.h"
#include "Form.h"
#include "SampleStatic.h"
#include <chrono>

using namespace cevent;

TestObject* test = new TestObject();
TestObject* testStatic = new TestObject();
TestObject* testMore = new TestObject();
Form* form = new Form();
Form* form2 = new Form();
Sample* sample = new Sample();

static bool delegateStaticLoad(void* obj, const EventArgs* args) {
	printf("Sample::delegateStaticLoad [%i]\n", obj);
	testStatic->loadEvent -= new EventStartLoadListener(&delegateStaticLoad, form);
	printf("	delete current event\n");
	testMore->loadEvent -= new EventStartLoadListener(&Form::delegateLoad, form);
	printf("	delete next event\n");
	testMore->loadEvent += new EventStartLoadListener(sample, &Sample::delegateLoad, form); // pushed it on the end of the EventsChain, and it will be called in this EventChainCaller
	printf("	testMore->loadEvent: subscribe with delegate: sample->delegateLoad \n");
	return false;
}

int main(int argc, char* argv[])
{
// ignore this
#ifdef _DEBUG
	int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
	//flag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF; 
	_CrtSetDbgFlag(flag);
	// _CrtSetBreakAlloc(dump_nbr);
#endif
	test->loadEvent += new EventStartLoadListener(form, &Form::delegateLoad, form);
	test->loadEvent += new EventStartLoadListener(form, &Form::delegateLoad, form);

	test->completeLoadEvent += new EventCompleteLoadListener(form, &Form::delegateComliteLoad, form);
	test->completeLoadEvent -= new EventCompleteLoadListener(form, &Form::delegateComliteLoad, form2);
	test->completeLoadEvent += new EventCompleteLoadListener(form, &Form::delegateComliteLoad, form2);
	test->completeLoadEvent -= new EventCompleteLoadListener(&Form::delegateComliteLoad, form2);
	test->completeLoadEvent += new EventCompleteLoadListener(&Form::delegateComliteLoad, form2);
	test->completeLoadEvent += new EventCompleteLoadListener(&Sample::delegateStaticLoadComplite, form);

	testStatic->loadEvent += new EventStartLoadListener(&delegateStaticLoad, form);
	testStatic->completeLoadEvent--;

	testMore->loadEvent += new EventStartLoadListener(&Form::delegateLoad, form);
	
	EventListener* someEvent = new EventStartLoadListener(sample, &Sample::delegateLoad, form);
	printf("someEvent: subscribe with delegate: sample->delegateLoad \n");

	form->Loading("loading form");
	form2->Loading("loading form2");
	printf("loading form again\n");
	form->Loading("loading form");
	
	DELETE(test);
	DELETE(testStatic);
	DELETE(testMore)
	DELETE(form);
	DELETE(form2);
	DELETE(sample);
	DELETE(someEvent);
	system("pause");
}
